package com.spark.common.widget

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.support.annotation.StyleRes
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.RelativeLayout
import android.widget.TextView
import com.blankj.utilcode.util.ConvertUtils
import com.spark.common.R
import io.reactivex.annotations.NonNull

/**
 * 驳回，拒绝
 */
class RejectAlert(@NonNull context: Context, @StyleRes themeResId: Int) : Dialog(context, themeResId) {
    private constructor(@NonNull context: Context) : this(context, 0)

    class Builder(var context: Context,
                  var message: String? = "",
                  var hint: String = "") {

        private var contentView: View? = null
        private var etInfo: EditText? = null

        private var cancelText: String? = "取消"
        private var affirmText: String? = "确认"
        private var cancelListener: DialogInterface.OnClickListener? = null
        private var affirmClickedListener: OnAffirmClickedListener? = null

        fun contentView(v: View): Builder {
            contentView = v
            return this
        }

        fun cancel(cancelText: String? = "", listener: (DialogInterface, Int) -> Unit): Builder {
            if (!cancelText.isNullOrBlank())
                this.cancelText = cancelText
            cancelListener = DialogInterface.OnClickListener { dialog, which -> listener(dialog, which) }
            return this
        }

        fun affirm(affirmText: String? = "", listener: (DialogInterface, String?) -> Unit): Builder {
            if (!affirmText.isNullOrBlank())
                this.affirmText = affirmText
            this.affirmClickedListener = object : OnAffirmClickedListener {
                override fun onAffirm(alert: DialogInterface, text: String?) {
                    listener(alert, text)
                }
            }
            return this
        }

        fun create(): RejectAlert {
            val view = LayoutInflater.from(context).inflate(R.layout.layout_refuse_alert, null)
            val alert = RejectAlert(context, R.style.AlertStyle)
            alert.addContentView(view, RelativeLayout.LayoutParams(ConvertUtils.dp2px(250F), RelativeLayout.LayoutParams.WRAP_CONTENT))
            alert.setCanceledOnTouchOutside(false)
            alert.setCancelable(false)

            val tvAlertMessage = view.findViewById<TextView>(R.id.tv_alert_message)
            val tvCancel = view.findViewById<TextView>(R.id.tv_cancel)
            val tvAffirm = view.findViewById<TextView>(R.id.tv_affirm)
            etInfo = view.findViewById(R.id.et_info)

            tvCancel.text = cancelText
            tvAffirm.text = affirmText
            tvAlertMessage.text = message
            etInfo?.hint = hint

            tvCancel.setOnClickListener { cancelListener?.onClick(alert, DialogInterface.BUTTON_NEGATIVE) }
            tvAffirm.setOnClickListener {
                affirmClickedListener?.onAffirm(alert, etInfo?.text?.toString()?.trim())
            }

            return alert
        }
    }

    interface OnAffirmClickedListener {
        fun onAffirm(alert: DialogInterface, text: String?)
    }
}